var QQMapWX = require('../qqmap/qqmap-wx-jssdk.min');
var qqmapsdk = new QQMapWX({
    key: 'FBJBZ-2ZLC3-W4U3L-36L33-K5II7-QSBO7' // 必填
})

// 设置当前位置 promise 对象就是等执行完再返回
export const  getLocationInfo= () =>{
  return new Promise((resolve, reject)=>{
    wx.getLocation({
      type: "gcj02",
      success:function(resp){ 
        qqmapsdk.reverseGeocoder({
          location:{ latitude: resp.latitude, longitude: resp.longitude},
          success: function(res) {
            let locationInfo = { 
              latitude: resp.latitude, 
              longitude: resp.longitude,
              title: res.result.formatted_addresses.recommend,
              province: res.result.address_component.province,
              city: res.result.address_component.city
            }
            resolve(locationInfo)
          }
        })
      }
    })
  }) 
}

// 获取指定经纬度（目的地） 位置信息
export const  getDestinationInfo = (latitude, longitude) => {
  return new Promise((resolve, reject)=>{
    qqmapsdk.reverseGeocoder({
      location: { latitude, longitude},
      success: function(resp) {
        console.log(resp)
        qqmapsdk.calculateDistance({
          mode: 'driving',//行车距离
          to: [{latitude, longitude}], //终
          success: function(res) {
            let locationInfo = { 
              title: resp.result.formatted_addresses.recommend,
              region: resp.result.address_reference.town.title,
              distance: res.result.elements[0].distance,
              duration: Math.ceil(res.result.elements[0].duration/60)
            }
            resolve(locationInfo)
          },
          fail: function(error) {
            console.error(error);
          },
        })
      }
    })
  })
}
 